package org.vision.contactsmanager;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.vision.contactsmanager.bean.MyContacts;
import org.vision.contactsmanager.dao.ContactsManagerDbAdater;
import org.vision.contactsmanager.ui.SearchContactsAdapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchContacts extends Activity {

    //组件
    private ListView contactsListView;
    private AutoCompleteTextView textView;

    private TextWatcher autoCompleteTextWatcher;

    private ContactsManagerDbAdater contactsManagerDbAdapter;
    //列表的适配器
    private SearchContactsAdapter searchContactsAdapter;

    private List<MyContacts> myContacts;
    private String[] autoContacts;
    //ListView的点击事件
    private OnItemClickListener listItemClickListener;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.searchcontact);
        //初始化组件
        initComponents();
        //初始化监听器
        initLinsteners();
    }

    /**
     * 初始化全部联系人作为列表信息
     */
    private void initAllDatas() {
        myContacts = contactsManagerDbAdapter.getAllContacts();
    }

    private void initComponents() {
        contactsListView = (ListView) findViewById(R.id.list);
        contactsListView.setCacheColorHint(0);//拖动时避免出现黑色
        contactsListView.setDivider(null);//去掉每项下面的黑线(分割线)
        
        initContactsListViewData();
        textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        //绑定AUTOCOMPLETE
        //编辑AUTOCOMPLETE数组
        autoContacts = new String[myContacts.size()];
        for (int i = 0; i < myContacts.size(); i++) {
            autoContacts[i] = myContacts.get(i).getName() + "(" + myContacts.get(i).getTelPhone() + ")";
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchContacts.this,
                android.R.layout.simple_dropdown_item_1line, autoContacts);
        textView.setAdapter(adapter);
    }

    private void initLinsteners() {
        //单击联系人列表是直接打电话
        listItemClickListener=new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView phoneNumberTv = (TextView) view.findViewById(R.id.phoneNumber);
                Uri uri = Uri.parse("tel:" + phoneNumberTv.getText());
                Intent it = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(it);
            }
        };
        contactsListView.setOnItemClickListener(listItemClickListener);
        
        autoCompleteTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("autoCompleteTextWatcher", "beforeTextChanged");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String autoText = s.toString();
                if (autoText.length() >= 13) {
                    Pattern pt = Pattern.compile("\\(([1][3,5,8]+\\d{9})\\)");
                    Matcher mc = pt.matcher(autoText);
                    if (mc.find()) {
                        String sNumber = mc.group(1);
                        //更新列表，查找集合中更搜索的一样的集合
                        List<MyContacts> myContactsRs = new ArrayList<MyContacts>();
                        for (int i = 0; i < myContacts.size(); i++) {
                            if (sNumber.equals(myContacts.get(i).getTelPhone())) {
                                myContactsRs.add(myContacts.get(i));
                            }
                        }
                        //刷新列表
                        searchContactsAdapter.setItemList(myContactsRs);
                        searchContactsAdapter.notifyDataSetChanged();
                        dealWithAutoComplete(myContacts, sNumber);

                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("autoCompleteTextWatcher", "afterTextChanged");
            }
        };
        textView.addTextChangedListener(autoCompleteTextWatcher);
    }

    //处理搜索框选中的手机号
    private void dealWithAutoComplete(List<MyContacts> list, String un) {
        //点击后弹出电话
        Uri uri = Uri.parse("tel:" + un);
        Intent it = new Intent(Intent.ACTION_DIAL, uri);
        startActivity(it);
    }

    private void initContactsListViewData() {
        contactsManagerDbAdapter = new ContactsManagerDbAdater(this);
        contactsManagerDbAdapter.open();
        //获取该页面需要的数据
        initAllDatas();
        searchContactsAdapter = new SearchContactsAdapter(SearchContacts.this, myContacts);
        contactsListView.setAdapter(searchContactsAdapter);
    }
}
